package com.java.base.exercise.iostream.udp;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @Author RenPu
 * @Date 2024/2/3 10:09
 * @Version 1.0
 * @Description: UDP案例协议的编写 -服务端
 **/
@Slf4j
public class UDPServer {
    @SneakyThrows
    public static void main(String[] args) {

        DatagramSocket socket = new DatagramSocket(9876); // 创建DatagramSocket
        log.info("UDP 服务启动成功");
        byte[] buffer = new byte[1024]; // 创建接收数据的缓冲区
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length); // 创建DatagramPacket
        socket.receive(packet); // 接收数据报文
        String received = new String(packet.getData(), 0, packet.getLength()); // 将数据转换为字符串
       log.info("Received message: " + received); // 打印接收到的消息
    }



}
